package fr.nts.framework.database.jpa.dao;

import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import fr.nts.framework.database.jpa.model.security.SecurityUser;

/**
 * Un DAO manipulant les {@link SecurityUser} avec JPA
 * 
 * @author sco
 * @version 1.0.0
 */
@Component
public class SecurityUserDao extends AJPADao<SecurityUser, Long>{

	private final static String LOGIN = "login";
	private final static String PASSWORD = "password";
	private final static String ACTIVATED = "actif";
	
	private final static  Logger log = LoggerFactory.getLogger(SecurityUserDao.class);
	
	/*
	 * (non-Javadoc)
	 * @see fr.letrader.database.dao.ADao#getBoClass()
	 */
	@Override
	protected Class<SecurityUser> getBoClass(){
		return SecurityUser.class;
	}
	
	
	/**
	 * Récupère un utilisateur NTS via son login
	 * 
	 * @param login Le login de l'utilisateur
	 * @return Un utilisateur {@link SecurityUser}
	 */
	public SecurityUser findByLogin(final String login){
		SecurityUser user = null;
		
    	CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    	CriteriaQuery<SecurityUser> query = builder.createQuery(getBoClass());
    	
    	// Requète de base: select * from T
    	Root<SecurityUser> root = query.from(getBoClass());
    	query.where(builder.equal(root.get(LOGIN), 
    			                  login));
    	
        try{
        	user = getEntityManager().createQuery(query)
                                     .getSingleResult();
        }catch(NoResultException e){
			// non trouvé
		}
        
        return user;
	}
	
	
	/**
	 * Récupère un utilisateur NTS via son login et password
	 * 
	 * @param login Le login de l'utilisateur
	 * @param password Le password de l'utilisateur
	 * @return Un utilisateur {@link SecurityUser}
	 */
	public SecurityUser findByLoginAndPassword(final String login,
			                              final String password){
		SecurityUser user = null;
		
    	CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    	CriteriaQuery<SecurityUser> query = builder.createQuery(getBoClass());
    	
    	// Requète de base: select * from T
    	Root<SecurityUser> root = query.from(getBoClass());
    	query.where(builder.equal(root.get(LOGIN), 
    			                  login), 
    			    builder.equal(root.get(PASSWORD), 
    			    		      password),
			    	builder.equal(root.get(ACTIVATED), 
			   		              1));
        try{
        	user = getEntityManager().createQuery(query)
                                     .getSingleResult();
        }catch(NoResultException e){
			// non trouvé
		}
        
        return user;
	}
	
	
	// GETTER / FLUX
	public Logger getLog(){
		return log;
	}
}